import { createWebHistory, createRouter } from 'vue-router'
import Layout from '@/layout'
import { TrendCharts } from '@element-plus/icons-vue'

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/sociallogin',
    component: () => import('@/views/socialLogin'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '/error',
    component: () => import('@/views/error/Error'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/workbench',
    children: [
      {
        path: 'workbench',
        component: () => import('@/views/user/UserBehavior.vue'),
        name: 'workbench',
        meta: {
          title: "用户行为分析",
          icon: "UserBehavior"
        }
      }
      
    ]
  },
  {
    path: '/engine',
    name: 'EngineModule',
    component: Layout,
    meta: { title: '数据引擎', icon: 'engine' },
    children: [
      {
        path: 'data',
        name: 'DataEngine',
        component: () => import('@/views/engine/DataEngine.vue'),
        meta: { title: '数据源配置' }
      },
      {
        path: 'catalog',
        name: 'DataCatalog',
        component: () => import('@/views/engine/DataCatalog.vue'),
        meta: { title: '数据目录管理' }
      }
    ]
  },
  {
    path: '/field',
    name: 'FieldMetadata',
    component: Layout,
    children: [
      {
        path: 'metadata',
        name: 'FieldMetadataPage',
        component: () => import('@/views/field/FieldMetadata.vue'),
        meta: { title: '字段元数据', icon: 'field' },
      }
    ]
  },
  {
    path: '/analysis',
    name: 'DataAnalysis',
    component: Layout,
    meta: { title: '数据分析', icon: 'Histogram' },
    children: [
      {
        path: 'process',
        name: 'DataAnalysisProcess',
        component: () => import('@/views/analysis/DataAnalysis.vue'),
        meta: { title: '数据处理与分析' }
      },
      {
        path: 'advanced',
        name: 'AdvancedAnalysis',
        component: () => import('@/views/analysis/AdvancedAnalysis.vue'),
        meta: { title: '高级分析' }
      }
    ]
  },
  {
    path: '/model',
    name: 'ModelModule',
    component: Layout,
    children: [
      {
        path: 'manage',
        name: 'ModelManage',
        component: () => import('@/views/model/ModelManage.vue'),
        meta: { title: '模型管理', icon: 'model' },
      }
    ]
  },
  {
    path: '/template',
    name: 'TemplateModule',
    component: Layout,
    children: [
      {
        path: 'manage',
        name: 'TemplateManage',
        component: () => import('@/views/template/TemplateManage.vue'),
        meta: { title: '模板管理', icon: 'Document' },
      }
    ]
  },
  {
    path: '/chart',
    name: 'ChartModule',
    component: Layout,
    children: [
      {
        path: 'dashboard',
        name: 'ChartDashboard',
        component: () => import('@/views/chart/ChartDashboard.vue'),
        meta: { title: '图表展示', icon: 'PieChart' },
      }
    ]
  }

]

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_ROUTER_PREFIX),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router
